package com.base.frame.baseframe.mybatistool.generator.first;

import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.internal.DefaultShellCallback;
import org.springframework.core.io.ClassPathResource;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName com.base.frame.baseframe.mybatistool.generator.first.MyBatisGeneratorMain
 * @Author FangShanShan
 * @Description java main 方法方式 自动生成问题  [mybatis自动生成代码方式一]
 * @Date 2020/5/9 12:49
 */
public class MyBatisGeneratorMain {
    public static void main(String[] args) {
        try {
            List<String> warnings = new ArrayList<String>();

            System.out.println(System.getProperty("user.dir"));
            boolean overwrite = true;
            // 如果放到resources目录用下面的方式读取文件
            ClassPathResource classPathResource = new ClassPathResource("/mybatis-generator/first/generatorConfig.xml");
            File configFile = classPathResource.getFile();
            /*String ss = MyBatisGeneratorMain.class.getClassLoader().getResource("").getFile();
            System.out.println("MyBatisGeneratorMain.main" + ss);

            File configFile = new File(ss);*/

            System.out.println("MyBatisGeneratorMain.main" + configFile.getPath());
//            String ss = MyBatisGeneratorMain.class.getClassLoader().getResource("").getFile();
//
//            File configFile = new File("classpath:mybatis-generator/first/generatorConfig.xml");
            ConfigurationParser cp = new ConfigurationParser(warnings);
            Configuration config = cp.parseConfiguration(configFile);
            DefaultShellCallback callback = new DefaultShellCallback(overwrite);
            MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
            myBatisGenerator.generate(null);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}




